package com.chx.controller;

import com.chx.constant.JwtClaimsConstant;
import com.chx.dto.UserDto;
import com.chx.dto.UserLoginDto;
import com.chx.dto.UserPageQueryDto;
import com.chx.dto.UserPasswordDto;
import com.chx.entity.Users;
import com.chx.properties.JwtProperties;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.UserService;
import com.chx.utils.JwtUtil;
import com.chx.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author cp-月入过万
 * @Date 2025/5/
 * 用户管理
 */
@RestController
@RequestMapping("/admin/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<UserVo> login(@RequestBody UserLoginDto userLoginDto) {
        log.info("用户登录:{}", userLoginDto);
        Users users = userService.login(userLoginDto);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, users.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);

        //封装返回结果
        UserVo userVo = UserVo.builder()
                .id(users.getUserId())
                .userName(users.getUsername())
                .name(users.getRealName())
                .token(token)
                .build();

        return Result.success(userVo);
    }

    /**
     * 退出
     */
    @ApiOperation(value = "用户退出")
    @PostMapping("/logout")
    public Result<String> logout() {return Result.success();}

    /**
     * 新增员工
     */
    @ApiOperation(value = "新增用户")
    @PostMapping
    public Result<String> save(@RequestBody UserDto userDto) {
        log.info("新增员工，员工数据：{}", userDto);
        userService.save(userDto);
        return Result.success();
    }

    /**
     * 修改用户信息
     */
    @ApiOperation(value = "修改用户信息")
    @PutMapping
    public Result<String> update(@RequestBody UserDto userDto) {
        log.info("修改员工信息：{}", userDto);
        userService.update(userDto);
        return Result.success();
    }

    /**
     * 禁用启用用户
     */
    @ApiOperation(value = "禁用启用用户")
    @PostMapping("/status/{status}/{id}")
    public Result<String> updateStatus(@PathVariable Integer status,@PathVariable Long id) {
        log.info("禁用启用用户：{}", id);
        userService.updateStatus(status, id);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @ApiOperation(value = "重置用户密码")
    @PostMapping("/password/{id}")
    public Result<String> resetPassword(@PathVariable Long id) {
        log.info("重置用户密码：{}", id);
        userService.resetPassword(id);
        return Result.success();
    }

    /**
     * 根据id查询用户
     */
    @ApiOperation(value = "根据id查询用户")
    @GetMapping("/{id}")
    public Result<Users> getById(@PathVariable Long id) {
        log.info("根据id查询用户：{}", id);
        Users users = userService.getById(id);
        return Result.success(users);
    }

    /**
     * 分页查询用户
     */
    @ApiOperation(value = "分页查询用户")
    @GetMapping("/page")
    public Result<PageResult> page(UserPageQueryDto userPageQueryDto) {
        log.info("分页查询用户：{}", userPageQueryDto);
        PageResult pageResult = userService.page(userPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码")
    @PutMapping("/password")
    public Result<String> updatePassword(@RequestBody UserPasswordDto userPasswordDto) {
        log.info("修改密码：{}", userPasswordDto);
        userService.updatePassword(userPasswordDto);
        return Result.success();
    }
}
